// +----------------------------------------------------------------------
// | Copyright (c) 2020 All rights reserved.
// +----------------------------------------------------------------------
// | User: Simony <1362229026@qq.com>
// | wechat: Shen86579595
// +----------------------------------------------------------------------

/**
 *
 * @param table
 * @param id_name
 * @param id_value
 * @param field
 * @param state
 * @param msg
 */
function changeTableVal(table, id_name, id_value, field, value, msg = '') {
    if (msg == '') msg = '操作';
    $.ajax({
        url: "/admin/common/changeTableVal",
        type: 'POST',
        dataType: 'JSON',
        data: {table: table, id_name: id_name, id_value: id_value, field: field, value: value, msg: msg},
        success: function (res) {
            if (res.status == 1) {
                layer.msg(res.msg, {icon: 1, time: 2500}, function () {
                    window.location.reload();
                });
            } else {
                layer.msg(res.msg, {icon: 5, time: 3000});
            }
        },
        error: function () {
            layer.msg('网络错误', {icon: 2, time: 3000});
        }
    });

}

/**
 * 添加
 * @param url
 */
function add(url, title = '内容管理', area = ['80%', '95%']) {
    layer.open({
        type: 2,
        title: title,
        shadeClose: false,
        shade: 0.2,
        area: area,
        skin: 'shangchuantup',
        // shift:-1,
        content: url,
    });
}


/**
 *
 * @param table
 * @param id
 * @param data
 */
function delById(table,id,data = {title: '', text: ''}){
    swal({
            title: '您确定要删除'+(data.title == undefined ? '' : data.title)+'吗？',
            text: (data.text == undefined || data.text == '') ? '删除后将无法恢复，请谨慎操作！' : data.text,
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "删除",
            cancelButtonText: "不删除",
        },
        function(isConfirm) {
            if (isConfirm) {
                var index = layer.load(1, {
                    // shade: [0.1,'#fff'] //0.1透明度的白色背景
                });
                ajaxFun("post",'/admin/common/delById',{id:id,table:table},'json',function(ret){
                    if(ret){
                        if(ret.ret){
                            layer.msg(ret.ret)
                            $("#id"+id).hide('fast',function(){
                                $("#id"+id).remove();
                            })
                        }else{
                            layer.msg(ret.err)
                        }
                    }else{
                        swal_msg("错误",ret,"error");
                    }
                    layer.close(index);
                })
            }
        })
}


function opTableValByID(url, id, value='') {
    $.ajax({
        url: url,
        type: 'POST',
        dataType: 'JSON',
        data: {id: id ,value: value},
        success: function (res) {
            if (res.status == 1) {
                layer.msg(res.msg, {icon: 1, time: 2500}, function () {
                    window.location.reload();
                });
            } else {
                layer.msg(res.msg, {icon: 5, time: 3000});
            }
        },
        error: function () {
            layer.msg('网络错误', {icon: 2, time: 3000});
        }
    });
}